<?php

namespace App\Admin\Services;

use App\Models\AdminOperationLog;

class OperationLogService
{
    /**
     * 獲取操作日誌列表
     * @param $input
     * @return mixed
     */
    public function getLogList($input)
    {
        $ip       = $input['ip'] ?? '';
        $staff_id = $input['staff_id'] ?? '';
        $time     = $input['time'] ?? '';
        $limit    = $input['limit'] ?? 20;

        return AdminOperationLog::query()
            ->when($time, function ($query, $time) {
                $timeStart = explode(' - ', $time)[0] ?? '';
                $timeEnd   = explode(' - ', $time)[1] ?? '';
                $timeEnd   = date('Y-m-d H:i:s', strtotime($timeEnd) + 86399);
                $query->whereBetween('operated_at', [$timeStart, $timeEnd]);
            })
            ->when($ip, function ($query, $ip) {
                $query->where('ip', $ip);
            })
            ->when($staff_id, function ($query, $staff_id) {
                $query->where('staff_id', $staff_id);
            })
            ->orderBy("operated_at", "desc")
            ->paginate($limit);
    }

}
